package NewUI;

import java.util.Vector;
import utils.KeyList;

/**
 * @author duncan
 */
public abstract class OrderPlaylist
{
    /**
     * Orders the specified list by name, and returns the modified list.
     *
     * @param list The list to rearrange.
     * @return A new, modified list.
     */
    public static Vector byName(Vector list, String key, boolean reverse)
    {
	//If the list contains less than 2 items,
	//then return the list without doing anything
	//since it's alredy in alphabetic order.
	if (list.size() < 2) return list;

	//If the method is still running, then go
	//thorugh all items in the list and check
	//if they should be one step afte rthe next.
	for (int i = 1; i < list.size(); i++)
	{
	    String val1 = (String) ((KeyList) list.get(i)).valueOf(key);
	    String val2 = (String) ((KeyList) list.get(i - 1)).valueOf(key);
	    if (reverse)
	    {
		if (val1.compareToIgnoreCase(val2) > 0)
		{
		    changePosition(list, i, i - 1);
		    return byName(list, key, reverse);
		}
	    }
	    else
	    {
		if (val1.compareToIgnoreCase(val2) < 0)
		{
		    changePosition(list, i, i - 1);
		    return byName(list, key, reverse);
		}
	    }
	}

	//Return the modified list.
	return list;
    }

    /**
     * Change the location of an item in a list of Strings. If there is already
     * something there, the items will swap locations with eachother.
     *
     * @param list The original list
     * @param index1 Index of the item to move
     * @param index2 Index of the position to place it.
     * @return A new modified list.
     */
    public static Vector changePosition(Vector list, int index1, int index2)
    {
	Object temp = list.get(index2);
	list.setElementAt(list.get(index1), index2);
	list.setElementAt(temp, index1);
	return list;
    }
}
